﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;

using Layers;

using NLog;

namespace HoldemGame
{

    public class BankRoll
    {
        private IObservableList<HoldemAction> _history;
        
        private XYChartDataList _bankroll;
        private int _processed = 0;
        private GamePlayer _player;


        private double Value;

        public IChartData<XY> Data { get { return _bankroll; } }

        public BankRoll(HoldemActionHistory history, GamePlayer p)
        {
            _player = p;
            Value = p.Bank;
            _history = history.FilterBy(delegate(HoldemAction ha)
            {
                BetAction ba = ha as BetAction;
                return null != ba && p.ID.Equals(ba.PlayerID);
            });
            _history.CollectionChanged += new NotifyCollectionChangedEventHandler(History_Changed);
            _bankroll = new XYChartDataList(p.ToString());
        }

        void History_Changed(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.Action == NotifyCollectionChangedAction.Reset)
            {
                _processed = 0;
                _bankroll.Clear();
            }
            else
            {
                Update(_processed, _history.Count-1);
                _processed = _history.Count;
            }
        }

        public void Update(int start, int last)
        {
            for(int i=start;i<=last;i++)
            {
                BetAction  ha = _history[i] as BetAction;
                Debug.Assert(null != ha && null != ha.Amount);
                Value += ha.BankDelta;
                Console.WriteLine("P={0} V={1}",_player.ID, Value);
                _bankroll.Add(ha.RunIndex+ha.Step*0.01,Value);
            }
        }
    }
}